package com.study_security.service.impl;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.study_security.entity.Project;
import com.study_security.service.ExportService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.awt.*;

@Service
public class ExportServiceImpl  implements ExportService {

    public byte[] exportProjectsToExcel(List<Project> projects) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Projects");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"项目ID", "项目名", "任务进度", "审核进度", "负责人ID", "开始日期", "结束日期", "预算", "合同金额", "重要级别", "项目类型", "项目状态"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        int rowNum = 1;
        for (Project project : projects) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(project.getProjectId());
            row.createCell(1).setCellValue(project.getProjectName());
            row.createCell(2).setCellValue(project.getTaskProgress().toString());
            row.createCell(3).setCellValue(project.getAuditProgress().toString());
            row.createCell(4).setCellValue(project.getResponsibleId());
            row.createCell(5).setCellValue(project.getStartDate().toString());
            row.createCell(6).setCellValue(project.getEndDate().toString());
            row.createCell(7).setCellValue(project.getBudget().toString());
            row.createCell(8).setCellValue(project.getContractAmount().toString());
            row.createCell(9).setCellValue(project.getImportantLevel());
            row.createCell(10).setCellValue(project.getProjectType());
            row.createCell(11).setCellValue(project.getProjectStatus());
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        return outputStream.toByteArray();
    }

//    public byte[] exportProjectsToPDF(List<Project> projects) throws IOException {
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        Document document = new Document();
//
//        try {
//            PdfWriter.getInstance(document, outputStream);
//            document.open();
//
//            // 添加标题
//            Font titleFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 14, Font.UNDERLINE);
//            Paragraph title = new Paragraph("Projects", titleFont);
//            title.setAlignment(Element.ALIGN_CENTER);
//            document.add(title);
//            document.add(Chunk.NEWLINE);
//
//            // 创建表格
//            PdfPTable table = new PdfPTable(12);
//            table.setWidthPercentage(100);
//
//            // 设置表头
//            Font headerFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
//            String[] headers = {"项目ID", "项目名", "任务进度", "审核进度", "负责人ID", "开始日期", "结束日期", "预算", "合同金额", "重要级别", "项目类型", "项目状态"};
//            for (String header : headers) {
//                PdfPCell headerCell = new PdfPCell();
//                headerCell.setBackgroundColor(new Color(173, 216, 230));
//                headerCell.setBorderWidth(1);
//                headerCell.setPhrase(new Phrase(header, headerFont));
//                table.addCell(headerCell);
//            }
//
//            // 填充数据
//            for (Project project : projects) {
//                table.addCell(String.valueOf(project.getProjectId()));
//                table.addCell(project.getProjectName());
//                table.addCell(String.valueOf(project.getTaskProgress()));
//                table.addCell(String.valueOf(project.getAuditProgress()));
//                table.addCell(String.valueOf(project.getResponsibleId()));
//                table.addCell(project.getStartDate().toString());
//                table.addCell(project.getEndDate().toString());
//                table.addCell(String.valueOf(project.getBudget()));
//                table.addCell(String.valueOf(project.getContractAmount()));
//                table.addCell(project.getImportantLevel());
//                table.addCell(project.getProjectType());
//                table.addCell(project.getProjectStatus());
//            }
//
//            document.add(table);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } finally {
//            document.close();
//        }
//
//        return outputStream.toByteArray();
//    }
}
